﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Collector
{
    public partial class ScanNum : Form
    {
        //public MesConnect mesServer;
        public ScanNum()
        {
            InitializeComponent();
        }

        public event PhoneNumberInputedEventHandler PhoneNumberInputed;
        private void OnPhoneNumberInputed(PhoneNumberInputedEventArgs e)
        {
            if (PhoneNumberInputed != null)
            {
                PhoneNumberInputed.Invoke(this, e);
            }
        }

        private void GMLK_Number_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData != Keys.Return)
            {
                return;
            }
            if(string.IsNullOrWhiteSpace(GMJK_Number.Text))
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageControl.Text = String.Format("未输入用户手机号码，请检查");
                }));
                GMJK_Number.Text = "";
                GMJK_Number.Focus();
                return;
            }

            string phoneNumberInfo = GMJK_Number.Text.Trim();
            if (phoneNumberInfo.Length != 11)
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageControl.Text = String.Format("请输入11位手机号码，请检查");
                }));
                GMJK_Number.Text = "";
                GMJK_Number.Focus();
                return;
            }
        }
        private void ScanNum_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.Close();
            //this.Dispose();
            //e.Cancel = true;
        }

        private void ScanSn_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
        private void btnEnter_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                this.button1_Click(sender, e);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string userPhoneNum = GMJK_Number.Text.Trim();
            bool bLeftHandSel = checkBox_LHand.Checked;
            bool bRightHandSel = checkBox_RHand.Checked;

            if (string.IsNullOrWhiteSpace(GMJK_Number.Text))
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageControl.Text = String.Format("未输入用户手机号码，请检查");
                }));
                return;
            }
            string phoneNumberInfo = GMJK_Number.Text.Trim();
            if (phoneNumberInfo.Length != 11)
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageControl.Text = String.Format("请输入11位手机号码，请检查");
                }));
                GMJK_Number.Text = "";
                GMJK_Number.Focus();
                return;
            }

            if (!bLeftHandSel && !bRightHandSel)
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageControl.Text = String.Format("请选择左手或者右手脉压收集，请检查");
                }));
                return;
            }

            OnPhoneNumberInputed(new PhoneNumberInputedEventArgs(userPhoneNum, bLeftHandSel, bRightHandSel));
            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //this.Close();
            //this.Close();
        }

        //bool bLeftSel = false;
        //bool bRightSel = false;
        private void checkBox_LHand_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox_LHand.Checked)
                checkBox_RHand.Checked = false;
        }
        private void checkBox_RHand_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox_RHand.Checked)
                checkBox_LHand.Checked = false;
        }

    }

    public delegate void PhoneNumberInputedEventHandler(object sender, PhoneNumberInputedEventArgs e);
    public class PhoneNumberInputedEventArgs : EventArgs
    {
        private string phoneNumber;
        private bool bLeftHand;
        private bool bRightHand;
        public PhoneNumberInputedEventArgs(string phoneNum, bool leftHand, bool rightHand)
        {
            this.phoneNumber = phoneNum;
            this.bLeftHand = leftHand;
            this.bRightHand = rightHand;
        }
        public bool LeftHand
        {
            get
            {
                return bLeftHand;
            }
        }
        public bool RightHand
        {
            get
            {
                return bRightHand;
            }
        }
        public string PhoneNumber
        {
            get
            {
                return phoneNumber;
            }
        }
    }
}
